/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PedidoDialog.java
 *
 * Created on Nov 9, 2010, 9:44:54 PM
 */
package br.com.tbp.gui;

import br.com.tbp.gui.table.ItensDePedidoTableModel;
import br.com.tbp.gui.table.SituacoesExternasTableModel;
import br.com.tbp.gui.table.JTableUtils;
import br.com.tbp.gui.table.SituacoesInternasTableModel;
import br.com.tbp.model.domain.Boc;
import br.com.tbp.model.domain.Pedido;
import br.com.tbp.model.domain.StatusPedido;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Date;
import javax.swing.JComponent;
import javax.swing.JRootPane;
import javax.swing.KeyStroke;

/**
 *
 * @author Diego Freire
 */
public class PedidoConsultorDialog extends javax.swing.JDialog {

    private ItensDePedidoTableModel model_itens_pedido;
    private SituacoesExternasTableModel model_situacoes_externas;
    private SituacoesInternasTableModel model_situacoes_internas;
    private Pedido pedido;
    private boolean novoPedido;
    private boolean add_info = true;
    private MenuPrincipal menu;

    public PedidoConsultorDialog(java.awt.Frame parent, String title, boolean modal) {
        super(parent, title, modal);
        initComponents();
        if (parent instanceof MenuPrincipal) {
            this.menu = (MenuPrincipal) parent;
        }
        posInit();
        okButton.requestFocus();
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        group1 = new javax.swing.ButtonGroup();
        tabbed1 = new javax.swing.JTabbedPane();
        basicoPanel = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jPanel43 = new javax.swing.JPanel();
        jPanel14 = new javax.swing.JPanel();
        infoLabel = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        consultorTextField = new javax.swing.JTextField();
        empresaTextField = new javax.swing.JTextField();
        idConsultorTexField = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        idEmpresaTexField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        idSupervisorTexField = new javax.swing.JTextField();
        supervisorTextField = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        idBocTexField = new javax.swing.JTextField();
        bocTextField = new javax.swing.JTextField();
        infoPanel = new javax.swing.JPanel();
        jPanel6 = new javax.swing.JPanel();
        jPanel8 = new javax.swing.JPanel();
        jPanel9 = new javax.swing.JPanel();
        scrollpane = new javax.swing.JScrollPane();
        itensDePedidoTable = new javax.swing.JTable();
        historicoPanel = new javax.swing.JPanel();
        tabbed2 = new javax.swing.JTabbedPane();
        internoPanel = new javax.swing.JPanel();
        jPanel21 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        situacoesInternasTable = new javax.swing.JTable();
        jPanel24 = new javax.swing.JPanel();
        jLabel18 = new javax.swing.JLabel();
        externoPanel = new javax.swing.JPanel();
        jPanel16 = new javax.swing.JPanel();
        jPanel17 = new javax.swing.JPanel();
        jSeparator1 = new javax.swing.JSeparator();
        jPanel18 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        situacoesExternasTable = new javax.swing.JTable();
        jPanel23 = new javax.swing.JPanel();
        jLabel17 = new javax.swing.JLabel();
        bocPanel = new javax.swing.JPanel();
        jLabel19 = new javax.swing.JLabel();
        planoField = new javax.swing.JTextField();
        jLabel20 = new javax.swing.JLabel();
        P2BField = new javax.swing.JTextField();
        jLabel21 = new javax.swing.JLabel();
        radarField = new javax.swing.JTextField();
        jLabel22 = new javax.swing.JLabel();
        situacaoField = new javax.swing.JTextField();
        jLabel23 = new javax.swing.JLabel();
        bocField = new javax.swing.JTextField();
        jLabel24 = new javax.swing.JLabel();
        dataInputField = new org.jdesktop.swingx.JXDatePicker();
        jPanel10 = new javax.swing.JPanel();
        jPanel12 = new javax.swing.JPanel();
        okButton = new javax.swing.JButton();
        jPanel13 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        msgLabel = new javax.swing.JLabel();
        empresaLabel = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        dataEmissaoTextField = new org.jdesktop.swingx.JXDatePicker();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        tabbed1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                tabbed1StateChanged(evt);
            }
        });

        basicoPanel.setLayout(new java.awt.BorderLayout());

        jPanel2.setLayout(new java.awt.BorderLayout());

        jPanel43.setLayout(new java.awt.BorderLayout());

        jPanel14.setPreferredSize(new java.awt.Dimension(910, 145));

        infoLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/tbp/resources/plus_16.png"))); // NOI18N
        infoLabel.setText("Informações");
        infoLabel.setToolTipText("Ver mais informações do cliente");
        infoLabel.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        infoLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                infoLabelMouseClicked(evt);
            }
        });

        jLabel2.setText("Consultor:");

        consultorTextField.setEditable(false);
        consultorTextField.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        consultorTextField.setFocusable(false);

        empresaTextField.setEditable(false);
        empresaTextField.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        empresaTextField.setFocusable(false);

        idConsultorTexField.setEditable(false);
        idConsultorTexField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        idConsultorTexField.setFocusable(false);

        jLabel9.setText("Empresa:");

        idEmpresaTexField.setEditable(false);
        idEmpresaTexField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        idEmpresaTexField.setFocusable(false);

        jLabel3.setText("Supervisor:");

        idSupervisorTexField.setEditable(false);
        idSupervisorTexField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        idSupervisorTexField.setFocusable(false);

        supervisorTextField.setEditable(false);
        supervisorTextField.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        supervisorTextField.setFocusable(false);

        jLabel10.setText("BOC:");

        idBocTexField.setEditable(false);
        idBocTexField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        idBocTexField.setFocusable(false);

        bocTextField.setEditable(false);
        bocTextField.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        bocTextField.setFocusable(false);

        javax.swing.GroupLayout jPanel14Layout = new javax.swing.GroupLayout(jPanel14);
        jPanel14.setLayout(jPanel14Layout);
        jPanel14Layout.setHorizontalGroup(
            jPanel14Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel14Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel14Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(infoLabel)
                    .addGroup(jPanel14Layout.createSequentialGroup()
                        .addGroup(jPanel14Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel9)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel10))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel14Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(idBocTexField)
                            .addComponent(idSupervisorTexField)
                            .addComponent(idEmpresaTexField)
                            .addComponent(idConsultorTexField, javax.swing.GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel14Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(bocTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 771, Short.MAX_VALUE)
                            .addComponent(supervisorTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 771, Short.MAX_VALUE)
                            .addComponent(empresaTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 771, Short.MAX_VALUE)
                            .addComponent(consultorTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 771, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanel14Layout.setVerticalGroup(
            jPanel14Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel14Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel14Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(idConsultorTexField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(consultorTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(4, 4, 4)
                .addGroup(jPanel14Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(idEmpresaTexField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(empresaTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel14Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(idSupervisorTexField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(supervisorTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel14Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(idBocTexField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bocTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(infoLabel)
                .addContainerGap())
        );

        jPanel43.add(jPanel14, java.awt.BorderLayout.PAGE_START);

        infoPanel.setLayout(new java.awt.BorderLayout());
        jPanel43.add(infoPanel, java.awt.BorderLayout.PAGE_END);

        jPanel2.add(jPanel43, java.awt.BorderLayout.PAGE_START);

        jPanel6.setLayout(new java.awt.BorderLayout());

        jPanel8.setLayout(new java.awt.BorderLayout());

        jPanel9.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 3, 0, 1));
        jPanel9.setLayout(new java.awt.BorderLayout());

        scrollpane.setPreferredSize(new java.awt.Dimension(453, 200));

        itensDePedidoTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        itensDePedidoTable.setSelectionBackground(new java.awt.Color(235, 169, 169));
        itensDePedidoTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        itensDePedidoTable.setShowHorizontalLines(false);
        scrollpane.setViewportView(itensDePedidoTable);

        jPanel9.add(scrollpane, java.awt.BorderLayout.CENTER);

        jPanel8.add(jPanel9, java.awt.BorderLayout.CENTER);

        jPanel6.add(jPanel8, java.awt.BorderLayout.CENTER);

        jPanel2.add(jPanel6, java.awt.BorderLayout.CENTER);

        basicoPanel.add(jPanel2, java.awt.BorderLayout.CENTER);

        tabbed1.addTab("Info. Básicas", basicoPanel);

        historicoPanel.setLayout(new java.awt.BorderLayout());

        tabbed2.setTabPlacement(javax.swing.JTabbedPane.LEFT);
        tabbed2.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                tabbed2StateChanged(evt);
            }
        });

        internoPanel.setLayout(new java.awt.BorderLayout());

        jPanel21.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 3, 1, 3));
        jPanel21.setLayout(new java.awt.BorderLayout());

        jScrollPane4.setPreferredSize(new java.awt.Dimension(453, 200));

        situacoesInternasTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        situacoesInternasTable.setSelectionBackground(new java.awt.Color(235, 169, 169));
        situacoesInternasTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        situacoesInternasTable.setShowHorizontalLines(false);
        jScrollPane4.setViewportView(situacoesInternasTable);

        jPanel21.add(jScrollPane4, java.awt.BorderLayout.CENTER);

        jLabel18.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/tbp/resources/right-azul.png"))); // NOI18N
        jLabel18.setText("Histórico de Situações Internas");

        javax.swing.GroupLayout jPanel24Layout = new javax.swing.GroupLayout(jPanel24);
        jPanel24.setLayout(jPanel24Layout);
        jPanel24Layout.setHorizontalGroup(
            jPanel24Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel24Layout.createSequentialGroup()
                .addContainerGap(694, Short.MAX_VALUE)
                .addComponent(jLabel18)
                .addContainerGap())
        );
        jPanel24Layout.setVerticalGroup(
            jPanel24Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel18, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        jPanel21.add(jPanel24, java.awt.BorderLayout.PAGE_START);

        internoPanel.add(jPanel21, java.awt.BorderLayout.CENTER);

        tabbed2.addTab("Interno", internoPanel);

        externoPanel.setLayout(new java.awt.BorderLayout());

        jPanel16.setLayout(new java.awt.BorderLayout());

        jPanel17.setLayout(new java.awt.BorderLayout());
        jPanel17.add(jSeparator1, java.awt.BorderLayout.PAGE_START);

        jPanel16.add(jPanel17, java.awt.BorderLayout.PAGE_START);

        jPanel18.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 3, 1, 3));
        jPanel18.setLayout(new java.awt.BorderLayout());

        jScrollPane2.setPreferredSize(new java.awt.Dimension(453, 200));

        situacoesExternasTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        situacoesExternasTable.setSelectionBackground(new java.awt.Color(235, 169, 169));
        situacoesExternasTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        situacoesExternasTable.setShowHorizontalLines(false);
        jScrollPane2.setViewportView(situacoesExternasTable);

        jPanel18.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        jLabel17.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/tbp/resources/right-verde.png"))); // NOI18N
        jLabel17.setText("Histórico de Situações Externas");

        javax.swing.GroupLayout jPanel23Layout = new javax.swing.GroupLayout(jPanel23);
        jPanel23.setLayout(jPanel23Layout);
        jPanel23Layout.setHorizontalGroup(
            jPanel23Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel23Layout.createSequentialGroup()
                .addContainerGap(690, Short.MAX_VALUE)
                .addComponent(jLabel17)
                .addContainerGap())
        );
        jPanel23Layout.setVerticalGroup(
            jPanel23Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel17, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        jPanel18.add(jPanel23, java.awt.BorderLayout.PAGE_START);

        jPanel16.add(jPanel18, java.awt.BorderLayout.CENTER);

        externoPanel.add(jPanel16, java.awt.BorderLayout.CENTER);

        tabbed2.addTab("Externo", externoPanel);

        jLabel19.setText("Plano");

        planoField.setEditable(false);
        planoField.setFocusable(false);

        jLabel20.setText("P2B");

        P2BField.setEditable(false);
        P2BField.setFocusable(false);

        jLabel21.setText("Radar");

        radarField.setEditable(false);
        radarField.setFocusable(false);

        jLabel22.setText("Situação");

        situacaoField.setEditable(false);
        situacaoField.setFocusable(false);

        jLabel23.setText("BOC");

        bocField.setEditable(false);
        bocField.setFocusable(false);

        jLabel24.setText("Data input");

        dataInputField.setEditable(false);

        javax.swing.GroupLayout bocPanelLayout = new javax.swing.GroupLayout(bocPanel);
        bocPanel.setLayout(bocPanelLayout);
        bocPanelLayout.setHorizontalGroup(
            bocPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(bocPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(bocPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(planoField, javax.swing.GroupLayout.DEFAULT_SIZE, 902, Short.MAX_VALUE)
                    .addComponent(jLabel19)
                    .addComponent(jLabel20)
                    .addComponent(P2BField, javax.swing.GroupLayout.DEFAULT_SIZE, 902, Short.MAX_VALUE)
                    .addComponent(jLabel21)
                    .addComponent(radarField, javax.swing.GroupLayout.DEFAULT_SIZE, 902, Short.MAX_VALUE)
                    .addComponent(situacaoField, javax.swing.GroupLayout.DEFAULT_SIZE, 902, Short.MAX_VALUE)
                    .addComponent(jLabel23)
                    .addComponent(bocField, javax.swing.GroupLayout.DEFAULT_SIZE, 902, Short.MAX_VALUE)
                    .addComponent(jLabel24)
                    .addComponent(dataInputField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel22))
                .addContainerGap())
        );
        bocPanelLayout.setVerticalGroup(
            bocPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(bocPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel19)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(planoField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel20)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(P2BField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel21)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(radarField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel24)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(dataInputField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel22)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(situacaoField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel23)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(bocField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(182, Short.MAX_VALUE))
        );

        tabbed2.addTab("BOC", bocPanel);

        historicoPanel.add(tabbed2, java.awt.BorderLayout.CENTER);

        tabbed1.addTab("Histórico", historicoPanel);

        getContentPane().add(tabbed1, java.awt.BorderLayout.CENTER);

        jPanel10.setBackground(new java.awt.Color(255, 255, 255));
        jPanel10.setLayout(new java.awt.BorderLayout());

        jPanel12.setBackground(new java.awt.Color(255, 255, 255));

        okButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/tbp/resources/accept.png"))); // NOI18N
        okButton.setText("OK");
        okButton.setPreferredSize(new java.awt.Dimension(130, 28));
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });
        jPanel12.add(okButton);

        jPanel10.add(jPanel12, java.awt.BorderLayout.LINE_END);

        jPanel13.setBackground(new java.awt.Color(255, 255, 255));
        jPanel10.add(jPanel13, java.awt.BorderLayout.LINE_START);

        getContentPane().add(jPanel10, java.awt.BorderLayout.PAGE_END);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setLayout(new java.awt.BorderLayout());

        jPanel4.setBackground(new java.awt.Color(255, 255, 255));

        msgLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/tbp/resources/venda32.png"))); // NOI18N
        msgLabel.setText("mensagem");
        jPanel4.add(msgLabel);

        empresaLabel.setFont(new java.awt.Font("Dialog", 1, 13)); // NOI18N
        empresaLabel.setForeground(new java.awt.Color(0, 102, 255));
        jPanel4.add(empresaLabel);

        jPanel1.add(jPanel4, java.awt.BorderLayout.LINE_START);

        jPanel5.setBackground(new java.awt.Color(255, 255, 255));
        jPanel5.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 1, 1, 1));

        jLabel4.setText("Data de Entrada:");
        jPanel5.add(jLabel4);

        dataEmissaoTextField.setEditable(false);
        dataEmissaoTextField.setFocusable(false);
        jPanel5.add(dataEmissaoTextField);

        jPanel1.add(jPanel5, java.awt.BorderLayout.LINE_END);

        getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_START);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void infoLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_infoLabelMouseClicked
        if (infoLabel.isEnabled()) {
            if (infoPanel.getComponentCount() > 0) {
                infoPanel.removeAll();
                infoPanel.repaint();
            }
            if (add_info) {
                infoPanel.add(new DetalhesDaEmpresaPanel(this.objetoPedido().getEmpresa()), "First");
                infoPanel.repaint();
                add_info = false;
            } else {
                add_info = true;
            }

            this.pack();
            this.setLocationRelativeTo(null);
        }
    }//GEN-LAST:event_infoLabelMouseClicked

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        dispose();
    }//GEN-LAST:event_okButtonActionPerformed

    private void tabbed2StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_tabbed2StateChanged
        this.atualizarBocInfo();
    }//GEN-LAST:event_tabbed2StateChanged

    private void tabbed1StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_tabbed1StateChanged
        int tab = tabbed1.getSelectedIndex();
        if (tab == 1) {
            this.atualizarBocInfo();
        }
    }//GEN-LAST:event_tabbed1StateChanged
    // <editor-fold defaultstate="collapsed" desc="Declaration of class members">
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField P2BField;
    private javax.swing.JPanel basicoPanel;
    private javax.swing.JTextField bocField;
    private javax.swing.JPanel bocPanel;
    private javax.swing.JTextField bocTextField;
    private javax.swing.JTextField consultorTextField;
    private org.jdesktop.swingx.JXDatePicker dataEmissaoTextField;
    private org.jdesktop.swingx.JXDatePicker dataInputField;
    private javax.swing.JLabel empresaLabel;
    private javax.swing.JTextField empresaTextField;
    private javax.swing.JPanel externoPanel;
    private javax.swing.ButtonGroup group1;
    private javax.swing.JPanel historicoPanel;
    private javax.swing.JTextField idBocTexField;
    private javax.swing.JTextField idConsultorTexField;
    private javax.swing.JTextField idEmpresaTexField;
    private javax.swing.JTextField idSupervisorTexField;
    private javax.swing.JLabel infoLabel;
    private javax.swing.JPanel infoPanel;
    private javax.swing.JPanel internoPanel;
    private javax.swing.JTable itensDePedidoTable;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel23;
    private javax.swing.JLabel jLabel24;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel10;
    private javax.swing.JPanel jPanel12;
    private javax.swing.JPanel jPanel13;
    private javax.swing.JPanel jPanel14;
    private javax.swing.JPanel jPanel16;
    private javax.swing.JPanel jPanel17;
    private javax.swing.JPanel jPanel18;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel21;
    private javax.swing.JPanel jPanel23;
    private javax.swing.JPanel jPanel24;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel43;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel msgLabel;
    private javax.swing.JButton okButton;
    private javax.swing.JTextField planoField;
    private javax.swing.JTextField radarField;
    private javax.swing.JScrollPane scrollpane;
    private javax.swing.JTextField situacaoField;
    private javax.swing.JTable situacoesExternasTable;
    private javax.swing.JTable situacoesInternasTable;
    private javax.swing.JTextField supervisorTextField;
    private javax.swing.JTabbedPane tabbed1;
    private javax.swing.JTabbedPane tabbed2;
    // End of variables declaration//GEN-END:variables
    // </editor-fold>

    public void setPedido(Pedido p) {
        this.pedido = p;
        dataEmissaoTextField.setDate(new Date(p.getData().getTime()));

        idConsultorTexField.setText(p.getConsultor() != null ? p.getConsultor().getId() + "" : null);
        consultorTextField.setText(p.getConsultor() != null ? p.getConsultor().getNome() : null);

        idEmpresaTexField.setText(p.getEmpresa() != null ? p.getEmpresa().getId() + "" : null);
        empresaTextField.setText(p.getEmpresa() != null ? p.getEmpresa().getRazaoSocial() + "      [" + p.getEmpresa().getCnpj() + "]" : null);

        idSupervisorTexField.setText(p.getSupervisor() != null ? p.getSupervisor().getId() + "" : null);
        supervisorTextField.setText(p.getSupervisor() != null ? p.getSupervisor().getNome() + "" : null);

        idBocTexField.setText(p.getBoc() != null ? p.getBoc().getId() + "" : null);
        bocTextField.setText(p.getBoc() != null ? p.getBoc().getNome() + "" : null);
        
        model_itens_pedido = new ItensDePedidoTableModel(p);
        itensDePedidoTable.setModel(model_itens_pedido);

        model_situacoes_externas = new SituacoesExternasTableModel(p);
        situacoesExternasTable.setModel(model_situacoes_externas);

        model_situacoes_internas = new SituacoesInternasTableModel(p);
        situacoesInternasTable.setModel(model_situacoes_internas);

        refreshItensDePedido();
        refreshSituacoesInternas();
        refreshSituacoesExternas();

        planoField.setText(p.getBocInfo().getPlano());
        P2BField.setText(p.getBocInfo().getP2B());
        radarField.setText(p.getBocInfo().getRadar());
        dataInputField.setDate(p.getBocInfo().getDataInput());
    }

    public boolean isNovoPedido() {
        return novoPedido;
    }

    public void setNovoPedido(boolean novoPedido) {
        this.novoPedido = novoPedido;
        if (isNovoPedido()) {
            msgLabel.setText("Forneça os dados para o novo pedido");
            empresaLabel.setText(null);
        } else {
            msgLabel.setText("Pedido nº " + this.objetoPedido().getId() + "  - ");
            empresaLabel.setText(this.objetoPedido().getEmpresa().getNomeFantasia());
        }
    }

    private Pedido objetoPedido() {
        return this.pedido;
    }

    private void refreshItensDePedido() {
        model_itens_pedido.fireTableDataChanged();
        JTableUtils.fitAllColumns(itensDePedidoTable);
    }

    private void refreshSituacoesExternas() {
        model_situacoes_externas.fireTableDataChanged();
        JTableUtils.fitAllColumnsWithoutRenderer(situacoesExternasTable);
    }

    private void refreshSituacoesInternas() {
        model_situacoes_internas.fireTableDataChanged();
        JTableUtils.fitAllColumnsWithoutRenderer(situacoesInternasTable);
    }

    @Override
    protected JRootPane createRootPane() {
        ActionListener closeListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                okButtonActionPerformed(e);
            }
        };
        KeyStroke stroke =
                KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
        JRootPane myRootPane = new JRootPane();
        myRootPane.registerKeyboardAction(
                closeListener, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);

        return myRootPane;
    }

    public MenuPrincipal getMenu() {
        return menu;
    }

    private void posInit() {
        setLocationRelativeTo(null);
    }

    private void atualizarBocInfo() {
        int tab = tabbed2.getSelectedIndex();
        if (tab == 2) {
            StatusPedido s = this.objetoPedido().getSituacaoExternaAtual();
            situacaoField.setText(s != null ? s.getNome() : null);
            Boc boc = this.objetoPedido().getBoc();
            bocField.setText(boc != null ? boc.getNome() : null);
            planoField.requestFocus();
        }
    }
}
